package com.lagou.utils;

import com.lagou.annotation.QjcAutowired;
import com.lagou.annotation.QjcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@QjcService
public class ConnectionUtils {

    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    // 从当前线程获取connection
    public Connection getCurrentThreadConnection() throws SQLException {
        Connection connection = threadLocal.get();// 从当前线程获取connection
        if (connection == null) {
            // 如果获取不到，就从连接池中获取连接，并放入threadLocal中
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }

    public void inti() {
        System.err.println("bean对象初始化方法");
    }

    public void destory() {
        System.err.println("bean对象销毁方法");
    }

}
